package com.gsabs.ihm;

import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JTextField;

public class ReachTextFiled extends JTextField {

	private LockListner lockListnerNum = new LockListner(
			LockListner.NUMEIC_TYPE);
	private LockListner lockListnerUpper = new LockListner(
			LockListner.UPPER_TYPE);

	public void setLocked(boolean b) {

		if (b) {
			addKeyListener(lockListnerNum);

		} else {
			removeKeyListener(lockListnerNum);
		}

	}

	public void setNumLock(boolean b) {

	}

	public void setUpper(boolean b) {

		if (b) {
			addKeyListener(lockListnerUpper);

		} else {
			removeKeyListener(lockListnerUpper);
		}

	}

	private class LockListner extends KeyAdapter {

		private int type;

		public static final int NUMEIC_TYPE = 0;
		public static final int UPPER_TYPE = 1;

		public LockListner(int type) {
			this.type = type;
		}

		public void keyTyped(KeyEvent e) {

			if (type == NUMEIC_TYPE) {
				char c = e.getKeyChar();

				if (c > '9' || c < '0') {
					e.setKeyChar((char) 0);
				}
			}

			else if (type == UPPER_TYPE) {

				char c = e.getKeyChar();

				if (c >= 'a' && c <= 'z') {
					e.setKeyChar((char) (c - 'a' + 'A'));
				}

			}

		}

	}

}
